package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.mappers.UserInfoMapper;
import com.tanhua.model.domain.UserInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.awt.SystemColor.info;

@DubboService
public class UserInfoApiImpl implements  UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo findById(Long id) {
        return userInfoMapper.selectById(id);
    }

    @Override
    public Map<Long, UserInfo> findByIds(List<Long> userIds, UserInfo info) {
        if (userIds == null || userIds.size() <= 0) {
            return new HashMap<>();
        }
        QueryWrapper qw = new QueryWrapper();
        //1、用户id列表
        qw.in("id", userIds);
        //2、添加筛选条件
        if (info != null) {
            if (info.getAge() != null) {
                qw.lt("age", info.getAge());
            }
            if (!StringUtils.isEmpty(info.getGender())) {
                qw.eq("gender", info.getGender());
            }
            if (!StringUtils.isEmpty(info.getNickname())) {
                qw.like("nickname", info.getNickname());
            }
        }
        List<UserInfo> list = userInfoMapper.selectList(qw);
        Map<Long, UserInfo> map = CollUtil.fieldValueMap(list, "id");
        return map;
    }

    @Override
    public IPage findAll(Integer page, Integer pagesize) {
        return userInfoMapper.selectPage(new Page<UserInfo>(page, pagesize), null);
    }

    //查询所有userinfo
    @Override
    public List<UserInfo> findAlls() {
        List<UserInfo> list = userInfoMapper.selectList(null);
        return list;
    }

    @Override
    public List<UserInfo> findIds(List<Long> userIds) {
        return null;
    }

    @Override
    public Map<Long, UserInfo> getByIds(Integer page, Integer pagesize, String nickname, List<Long> focus) {
        LambdaQueryWrapper<UserInfo> lqw =new LambdaQueryWrapper<>();
        lqw.in(UserInfo::getId,focus).like(StringUtils.isNotEmpty(nickname),UserInfo::getNickname,nickname);
        Page<UserInfo> userInfoPage = userInfoMapper.selectPage(new Page<UserInfo>(page, pagesize), lqw);

        List<UserInfo> records = userInfoPage.getRecords();

        Map<Long, UserInfo> map = CollUtil.fieldValueMap(records, "id");
        return map;
    }

    @Override
    public HashMap<Long, UserInfo> getByIds(List<Long> longs) {
        if (longs == null || longs.size() == 0) {
            return null;
        }
        //根据id数组查询
        LambdaQueryWrapper<UserInfo> lqw = new LambdaQueryWrapper<>();
        lqw.in(UserInfo::getId, longs);
        List<UserInfo> userInfos = userInfoMapper.selectList(lqw);
        HashMap<Long, UserInfo> userInfoMaps = new HashMap<>();
        //封装id和userInfo,方便后续操作
        for (UserInfo userInfo : userInfos) {
            userInfoMaps.put(userInfo.getId(), userInfo);
        }
        return userInfoMaps;
    }
}